cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        
        fruit_prefabs: {
            type: cc.Prefab,
            default: [],
        },
    },
    
    draw_line: function() {
        this.draw.clear();
        var begin = Math.floor(this.start_index);
        for(var i = begin; i < this.point_set.length - 1; i ++) {
            this.draw.lineWidth = 3;
            this.draw.strokeColor = cc.Color.GREEN;
            this.draw.moveTo(this.point_set[i].x,this.point_set[i].y);
            this.draw.lineTo(this.point_set[i + 1].x,this.point_set[i + 1].y);
            this.draw.stroke();
        }
    },
    
    filter_line: function() {
        if (this.point_set.length <= 10) {
            return;
        }
        
        if(this.start_index < this.point_set.length - 10) {
            this.start_index = this.point_set.length - 10;
        }
    },
    
    // 初始化
    onLoad: function () {
        this.node.addComponent(cc.Graphics);
        this.draw = this.node.getComponent(cc.Graphics);
        this.node_speed = 30;
        this.draw.x = -320;
        this.draw.y = -480;
        this.start_index = 0;
        this.in_touch = false;
        this.is_started = false;
        this.total_count = 0;
        this.split_count = 0;
        
        this.game_root = cc.find("UI_ROOT/game_root");
        this.start_menu = cc.find("UI_ROOT/start_menu");
        this.start_menu.active = true;
        
        this.score_root = cc.find("UI_ROOT/game_top");
        this.score_root.active = false;
        this.score_label = cc.find("UI_ROOT/game_top/score/str").getComponent(cc.Label);
        
        this.point_set = [];
        this.node.on(cc.Node.EventType.TOUCH_START, function(t){
            this.point_set = [];
            this.start_index = 0;
            this.in_touch = true;
            this.point_set.push(t.getLocation());
        }.bind(this), this.node);
        
        this.node.on(cc.Node.EventType.TOUCH_MOVE, function(t) {
            this.point_set.push(t.getLocation());
            this.filter_line();
            this.hit_split_fruit();
            
        }.bind(this), this.node);
        
        this.node.on(cc.Node.EventType.TOUCH_END, function(t) {
            this.point_set = [];
            this.in_touch = false;
            this.start_index = 0;
            this.draw.clear();
        }.bind(this), this.node);
        
    },
    
    start: function() {
        this.game_root.removeAllChildren();
        var sandia = cc.instantiate(this.fruit_prefabs[0]);
        this.game_root.addChild(sandia);
        sandia.x = 0;
        sandia.y = -180;
    },
    
    hit_fruit: function(w_box) {
        if (this.in_touch === false) {
            return false;
        }
        
        var begin = Math.floor(this.start_index);
        
        for(var i = this.point_set.length - 1; i >= begin; i --) {
            if(w_box.contains(this.point_set[i])) {
                return true;
            }
        }
        return false;
    },
    
    update_score: function() {
        this.score_label.string = "" + this.split_count;
    },
    
    gen_fruit: function() {
        var f_type = 5 * Math.random();
        f_type = Math.floor(f_type);
        if(f_type >= 5) {
            f_type = 4;
        }
        
        this.total_count ++;
        var fruit = cc.instantiate(this.fruit_prefabs[f_type]);    
        this.game_root.addChild(fruit);
        fruit.x = 0;
        fruit.y = -500;
        
        var r = 0.5 * Math.PI + ((Math.random() - 0.5) * Math.PI / 6)
        var speed = 1000;
        var vx = speed * Math.cos(r);
        var vy = speed * Math.sin(r);
        
        fruit.getComponent("throw_action").start_action(vx, vy);
        this.update_score();
        
        this.scheduleOnce(this.gen_fruit.bind(this), Math.random() * 2 + 1);
    },
    
    on_start_game: function() {
        if (this.is_started === true) {
            return;
        }
        this.is_started = true;
        this.start_menu.active = false;
        this.score_root.active = true;
        this.split_count = 0;
        this.update_score();
        this.unscheduleAllCallbacks();
        this.scheduleOnce(this.gen_fruit.bind(this), Math.random() * 2 + 1);
    },
    
    hit_split_fruit: function() {
        for(var i = 0; i < this.game_root.children.length; i ++) {
            var fruit = this.game_root.children[i];
            var fruit_com = fruit.getComponent("fruit");
            if(fruit_com.status_state !== 0) {
                continue;
            }
            
            var w_box = fruit.getBoundingBoxToWorld();
            if (this.hit_fruit(w_box)) {
                this.split_count ++;
                this.update_score();
                fruit.getComponent("throw_action").stop_action();
                
                var dir = this.point_set[this.point_set.length - 1].sub(this.point_set[Math.floor(this.start_index)]);
                var degree = Math.atan2(dir.y, dir.x) * 180 / Math.PI;
                fruit_com.split_fruit(360 - degree);
                this.point_set = [];
                this.start_index = 0;
                
                if (this.is_started === false) {
                    this.on_start_game();
                }
                return;
            }
        }  
    },
    
    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        
        if (this.in_touch) {
            var s = this.node_speed * dt;
            this.start_index += s;
            this.draw_line();
        }
    },
});
